/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uk.ac.rdg.resc.mmd.controllers;

import java.io.IOException;
import java.util.List;
import org.joda.time.DateTime;
import uk.ac.rdg.resc.mmd.colocation.NcModelTimeseries;
import uk.ac.rdg.resc.mmd.colocation.ObsProxyData;
import uk.ac.rdg.resc.mmd.ncwms.exceptions.InvalidDimensionValueException;

/**
 *
 * @author Alastair
 */
public class KmlController {

    public static String getHeader() {
        String headerStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        headerStr += "<kml xmlns=\"http://earth.google.com/kml/2.1\">\n";
        headerStr += "<Document><name>testData</name>\n";
        headerStr += "<Folder><name>Proxy Sites</name>\n";
        
        return headerStr;
    }
    
    public static String getFooter() {
        String footerStr = "</Folder>\n";
        footerStr += "</Document>\n";
        footerStr += "</kml>\n";
        
        return footerStr;
    }

    public static String streamKml(ObsProxyData obs, NcModelTimeseries model,
            String file, String modelLocation) throws InvalidDimensionValueException, IOException {

        float lat = obs.getLat();
        float lon = obs.getLon();
        List<DateTime> dates = obs.getDates();
        
        List<Double> elevations = model.getNonNullElevations();
        String elevationUnits = model.getElevationUnits();

        StringBuffer kmlStrBuf = new StringBuffer();

        DateTime startDate = dates.get(0);
        DateTime endDate = dates.get(dates.size() - 1);
        String startMonth = startDate.getMonthOfYear() < 10 ? "0" + startDate.getMonthOfYear() : "" + startDate.getMonthOfYear();
        String endMonth = endDate.getMonthOfYear() < 10 ? "0" + endDate.getMonthOfYear() : "" + endDate.getMonthOfYear();
        String startDay = startDate.getDayOfMonth() < 10 ? "0" + startDate.getDayOfMonth() : "" + startDate.getDayOfMonth();
        String endDay = endDate.getDayOfMonth() < 10 ? "0" + endDate.getDayOfMonth() : "" + endDate.getDayOfMonth();
        String startStr = startDate.getYear() + "-" + startMonth + "-" + startDay;
        String endStr = endDate.getYear() + "-" + endMonth + "-" + endDay;

        kmlStrBuf.append("<Placemark>\n");
        kmlStrBuf.append("<TimeSpan>\n");
        kmlStrBuf.append("<begin>" + startStr + "</begin>\n");
        kmlStrBuf.append("<end>" + endStr + "</end>\n");
        kmlStrBuf.append("</TimeSpan>\n");
        kmlStrBuf.append("<name></name>\n");
        kmlStrBuf.append("<description><![CDATA[");

        for (double elev : elevations) {
            kmlStrBuf.append("<a href=\"http://localhost:8084/MMDPortal/mmd" +
                    "?file=" + file +
                    "&model=" + modelLocation +
                    "&elevation=" + elev +
                    "&request=Show+Model+-+Obs+Timeseries+Plot" +
                    "\">" + elev + " " + elevationUnits + " Plot</a>");
            kmlStrBuf.append("<br/>");
        }

        kmlStrBuf.append("]]></description>\n");
        kmlStrBuf.append("<Style>\n");
        kmlStrBuf.append("<BalloonStyle>\n");
        kmlStrBuf.append("<text>$[description]</text>\n");
        kmlStrBuf.append("</BalloonStyle>\n");
        kmlStrBuf.append("<IconStyle>\n");
        kmlStrBuf.append("<Icon>\n");
        kmlStrBuf.append("<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n");
        kmlStrBuf.append("</Icon>\n");
        kmlStrBuf.append("</IconStyle>\n");
        kmlStrBuf.append("</Style>\n");
        kmlStrBuf.append("<Point>\n");
        kmlStrBuf.append("<coordinates>" + lon + "," + lat + "</coordinates>\n");
        kmlStrBuf.append("</Point>\n");
        kmlStrBuf.append("</Placemark>\n");

        return kmlStrBuf.toString();
    }
}
